﻿Public Class AquariumList
    Inherits ItemGridPage

    Protected Overrides ReadOnly Property DefaultOrder() As OrderCriterion
        Get
            Return New OrderCriterion("Title")
        End Get
    End Property

    Protected Overrides ReadOnly Property DefaultFilter() As FilterCriterion
        Get
            Dim tag As String = Request.QueryString.Get("Tag")
            Dim uname As String = Request.QueryString.Get("UserName")
            If Not String.IsNullOrWhiteSpace(tag) Then
                Return New FilterCriterion("Tag", tag)
            ElseIf Not String.IsNullOrWhiteSpace(uname) Then
                Return New FilterCriterion("MemberUserName", uname)
            Else
                Return New FilterCriterion("Title", "")
            End If
        End Get
    End Property

    Protected Overrides ReadOnly Property CreatePageURL As String
        Get
            Return "~/Pages/AquariumDetail.aspx?Action=Add"
        End Get
    End Property

    Protected Overrides Function CreateDataGateway() As DataGateway
        Return New AquariumGateway()
    End Function

    Protected Function GetAquariumTags(ByVal aquariumID As ULong) As DataTable
        Dim gateway As New AquariumTagGateway()
        Return gateway.FindAquariumTags(aquariumID)
    End Function

    Protected Function GetTagFontUnit(ByVal tag As String, ByVal weight As Integer) As FontUnit
        Return TagUtils.GetFontUnit(weight)
    End Function

    Protected Sub SearchLinkButton_Click(sender As LinkButton, e As EventArgs)
        ResetFilter(New FilterCriterion("Tag", sender.CommandArgument))
        UpdateDataSource()
    End Sub

    Protected Overrides Sub UpdateDataSource()
        If FilterBy.Column = "Tag" AndAlso Not String.IsNullOrWhiteSpace(FilterBy.Value) Then
            Dim aquariumGateway As New AquariumGateway()
            ListGrid.DataSource = aquariumGateway.FindByTag(FilterBy.Value)
            ListGrid.DataBind()
        Else
            MyBase.UpdateDataSource()
        End If
        ' Always update the tag cloud
        Dim tagGateway As New AquariumTagGateway()
        TagCloudRepeater.DataSource = tagGateway.FindTagsWithWeights()
        TagCloudRepeater.DataBind()
    End Sub
End Class